#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <windows.h>

int high,width;//尺寸
int bird_x,bird_y; 
int barl_xtop,barl_xdown,barl_y;//障碍物 
int bar2_xtop,bar2_xdown,bar2_y;//障碍物2 
int score;
int time;

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x,int y)
{
	HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}

void startup()
{
	high=24;
	width=40;
	
	bird_x=1;
	bird_y=3;
	
	barl_y=width;
	barl_xtop=high/4;
	barl_xdown=high/2;
	
	bar2_y=width+1;
	bar2_xtop=high/4+2;
	bar2_xdown=high/2+2;
	
	time=0;
	
	score=0;

	HideCursor(); 
}

void show()
{
	gotoxy(0,0);
	int i,j,k;
	if(barl_y<=width/2)
	{
		time++;
	}
	
	
	for(i=0;i<=high+1;i++)
	{
		for(j=0;j<=width;j++)
		{
			
			if(i==bird_x&&j==bird_y)
			{
				printf("@");//输出小鸟 
			}
		
			else if((j==barl_y)&&(i<=barl_xtop||i>=barl_xdown))//输出障碍物 
			{
				printf("*");
			}
			else if((j==bar2_y)&&(i<=bar2_xtop||i>=bar2_xdown))
			{
				if(time>=width/2)
				{
					printf("*");
				}
			}
			else if(i==high+1&&j<=width)
			{
				printf("-");
			 } 
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
printf("得分：%d\n",score);
}


void updatewithoutinput()
{
	//减慢小球速度
	static int speed=0;
	
	if(speed<4)
	{
		speed++;
	}
	if(speed==4)
	{
		if(bird_x==high||bird_x==0)//鸟撞死 
		{
			printf("game over!\n");
				exit(0);
		}	
		bird_x++;
		
		if(barl_y>0)
		{
			barl_y--;
		}
		else
		{
			barl_y=width;
			
			int randposition=rand()%(high-6);
			barl_xtop=randposition;
			barl_xdown=randposition+high/4;
		}
		
		if(time>0)
		{
			if(bar2_y>0)
		{
			bar2_y--;
		}
		else
		{
			bar2_y=width;
			
			int randposition2=rand()%(high-6);
			bar2_xtop=randposition2;
			bar2_xdown=randposition2+high/4;
		}
		}
		
		if(barl_y==bird_y)
		{
			if(bird_x>barl_xtop&&bird_x<barl_xdown)//加分 
			{
				score++;
			}
			else
			{
				printf("game over!\n");
				exit(0);
			
			}
		}
		
		if(bar2_y==bird_y)
		{
			if(bird_x>bar2_xtop&&bird_x<bar2_xdown)//加分 
			{
				score++;
			}
			else
			{
				printf("game over!\n");
				exit(0);
			
			}
		}
		
				
			
		speed=0;
	}		
	
}



void updatewithinput()
{
	char input;
	if(kbhit())
	{
		input=getch();
		
		if(input==' ')
		{
			bird_x=bird_x-2;
		}
		
		
	}	
	

}



int main()
{
	startup();
	while(1)
	{
		show();
		updatewithoutinput();
		updatewithinput();
	}
	return 0;
}